class people:
    def __init__(self, name, year, month, day, index):
        self.name = name
        self.year = int(year)
        self.month = int(month)
        self.day = int(day)
        self.index = index

    def __lt__(self, other):
        if self.year != other.year:
            return self.year < other.year
        elif self.month != other.month:
            return self.month < other.month
        elif self.day != other.day:
            return self.day < other.day
        else:
            return self.index > other.index

    def __repr__(self):
        return self.name


p = int(input())
n = []
y = []
m = []
d = []
obj_list = []
while 1:
    a = input().split(" ")
    n.append(a[0])
    y.append(a[1])
    m.append(a[2])
    d.append(a[3])
    if len(d) == p:
        break

for i in range(p):
    locals()['a' + str(i)] = people(n[i], y[i], m[i], d[i], i)
    obj_list.append(locals()['a' + str(i)])

obj_list.sort()
for obj in obj_list:
    print(obj)